package com.common.widget;

import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.utils.StarAppUtils;
import com.starappallcommon.R;

/**
 * 共通选择滚轮
 * 
 * <br>
 * Created 2014-9-3 下午2:49:36
 * 
 * @version
 * @author HuangYK
 * 
 * @see
 */
public class CommonSelectWheelPicker extends LinearLayout {

	/** 上下文 */
	private Context mContext;

	/** 选项滚轮 */
	private WheelView mWvOptions;

	/** 选项滚轮数据适配器 */
	private OptionsAdapter mOptionsAdapter;

	/** 文字大小 */
	private final int TEXT_SIZE = 18;
	/** 当前选中Item的字体颜色 */
	private static final int CUR_COLOR = 0xFF000000;
	/** 默认Item的字体颜色 */
	private static final int DEF_COLOR = 0xFFA4A4A4;

	/** 当前位置值 */
	private int mCurItemValue = 0;

	/**
	 * 初始化日期滚轮控件
	 * 
	 * @param context
	 *            上下文
	 */
	public CommonSelectWheelPicker(Context context) {
		this(context, null);
	}

	/**
	 * 
	 * 初始化日期滚轮控件
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 */
	public CommonSelectWheelPicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		// 显示的item数量
		final int visibleItemCount = 3;

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.common_select_wheel_picker, this, true);

		mWvOptions = (WheelView) findViewById(R.id.wv_options);
		mWvOptions.setVisibleItems(visibleItemCount);
		mWvOptions.setCyclic(false);
		mWvOptions.addScrollingListener(mOnWheelScrollListener);
	}

	/**
	 * 设置选项数据
	 * 
	 * <br>
	 * Created 2014-9-3 下午3:09:45
	 * 
	 * @param options
	 *            选项数据
	 * @author : HuangYK
	 */
	public void setOptions(String[] options, int curItem) {
		mCurItemValue = curItem;
		mOptionsAdapter = new OptionsAdapter(mContext, options, curItem);
		mWvOptions.setViewAdapter(mOptionsAdapter);
		mWvOptions.setCurrentItem(mCurItemValue);
	}

	/**
	 * 选项数据适配器
	 * 
	 * <br>
	 * Created 2014-9-3 下午3:04:21
	 * 
	 * @version CommonSelectWheelPicker
	 * @author HuangYK
	 * 
	 * @see
	 */
	private class OptionsAdapter extends ArrayWheelAdapter<String> {
		// Index of current item
		int currentItem;

		/**
		 * 
		 * Creates a new instance of OptionsAdapter. <br>
		 * Created 2014-5-5 下午2:57:01
		 * 
		 * @param context
		 *            上下文
		 * @param items
		 *            item数据
		 * @param calendar
		 *            当前日期
		 */
		public OptionsAdapter(Context context, String[] items, int curItem) {
			super(context, items);
			mCurItemValue = curItem;
			setTextSize(TEXT_SIZE);
			setTextColor(DEF_COLOR);
		}

		@Override
		protected void configureTextView(TextView view) {
			super.configureTextView(view);
			if (currentItem == mCurItemValue) {
				view.setTextColor(CUR_COLOR);
			}
			StarAppUtils.setViewTypeFace(view, true);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			cachedView = super.getItem(index, cachedView, parent);
			currentItem = index;
			if (currentItem == mCurItemValue) {
				((TextView) cachedView).setTextColor(CUR_COLOR);
			}else{
				((TextView) cachedView).setTextColor(DEF_COLOR);
			}
			return cachedView;
		}

		/**
		 * 刷新数据
		 * 
		 * <br>
		 * Created 2014-9-3 下午3:07:49
		 * 
		 * @author : HuangYK
		 */
		public void refresh() {
			notifyDataChangedEvent();
		}
	}

	/**
	 * 获取当前选中的位置
	 * 
	 * <br>
	 * Created 2014-9-3 下午4:12:50
	 * 
	 * @return 当前选中的位置
	 * @author : HuangYK
	 */
	public int getCurItemPosition() {
		return mCurItemValue;
	}

	/**
	 * 获取当前选中的文字
	 * 
	 * <br>
	 * Created 2014-9-3 下午4:14:43
	 * 
	 * @return 当前选中的文字
	 * @author : HuangYK
	 */
	public String getCurItemString() {
		return mOptionsAdapter.getItemText(mCurItemValue).toString();
	}

	/**
	 * 滚轮滚动状态监听
	 */
	private OnWheelScrollListener mOnWheelScrollListener = new OnWheelScrollListener() {

		@Override
		public void onScrollingStarted(WheelView wheel) {

		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			mCurItemValue = wheel.getCurrentItem();
			mWvOptions.setCurrentItem(mCurItemValue);
			if (mOptionsAdapter != null) {
				mOptionsAdapter.refresh();
			}
		}
	};

}
